<?php

	class DashboardHelper extends Helper {
		var $name = 'Dashboard';
		var $helpers = array('Form', 'Html');
		
		var $tab = "\t";
		var $nbsp = "&nbsp;";
		
		function taxonomyProductFormCategories($data, $level = 0) {
			return $this->productFormCategories($data, $level);
		}
		
		# deprecated
		private function productFormCategories($data, $level = 0) {
			$tabs = '' . str_repeat($this->tab, $level * 2);
			$li_tabs = $tabs . $this->tab;
			$output = '<input type="hidden" name="data[Taxonomy][Taxonomy][0]" value="0" />';
			$output.= $tabs. '<ul' . ($level != 0 ? ' class="children"' : ' class="categories"') . ' id="Categories">'."\n";
			foreach ($data as $row) {
				$output .= $li_tabs . '<li id="category-' . $row['Taxonomy']['id'] . '"><input type="checkbox" name="data[Taxonomy][Taxonomy][' . $row['Taxonomy']['id'] . ']" value="' . $row['Taxonomy']['id'] . '" id="TaxonomyTaxonomy' . $row['Taxonomy']['id'] . '" /> <label for="TaxonomyTaxonomy' . $row['Taxonomy']['id'] . '">' . $row['Taxonomy']['name'] . '</label>' . "\n";
				$output .= $this->Html->link(__('(editar)', true), array('controller' => 'taxonomies', 'action' => 'dashboard_edit', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)));
				if (isset($row['children'][0])) {
					$output .= $this->productFormCategories($row['children'], $level+1);
					$output .= $li_tabs . '</li>'."\n";
				}
				else {
					$output .= '</li>'."\n";
				}
			}
			$output .= $tabs . '</ul>'."\n";
			return $output;
		}
		
		function taxonomyFormSelectCategories($data, $level = 0)
		{
			$options = '';
			foreach ($data as $row) {
				$options.= '<option value="' . $row['Taxonomy']['id'] . '" ' . ($this->data['Taxonomy']['parent_id'] == $row['Taxonomy']['id'] ? 'selected="selected"' : '') . ' style="padding-left: ' . (5*($level+1)) . 'px;">' . $row['Taxonomy']['name'] . '</option>';
				if (isset($row['children'][0])) {
					$options.= $this->taxonomyFormSelectCategories($row['children'], $level+1);
				}
			}
			if ($level == 0) {
				$output = '<div class="input select">';
				$output.= '<label for="TaxonomyParentId">' . __('Categoría superior', true) . '</label>';
				$output.= '<select name="data[Taxonomy][parent_id]" id="TaxonomyParentId">';
				$output.= '<option value="0">' . __('Categoría superior', true) . '</option>';
				$output.= $options;
				$output.= '</select>';
				$output.= '</div>';
			}
			else {
				$output = $options;
			}
			
			return $output;
		}
		
		function taxonomyAdminCategories($data, $level = 0) {
			return $this->taxonomyAdmin($data, $level);
		}
	
		# deprecated
		private function taxonomyAdmin($data, $level = 0) {
			$tabs = '' . str_repeat($this->nbsp, $level * 4);
			$li_tabs = $tabs . $this->nbsp;
			$output = '';
			foreach ($data AS $row) {
				$output.= '<tr id="p' . $row['Taxonomy']['parent_id'] . '-c' . $row['Taxonomy']['id'] . '">';
				$output.= '<td>'
				. '<p>'
				. $li_tabs
				. '<span>' . $row['Taxonomy']['name'] . '</span>'
				;
				if ($row['Taxonomy']['parent_id'] == 0) {
					$output.= '<em>'
					. ' | '
					. $this->Html->link(__('Agregar sub-categoría', true), array('controller' => 'taxonomies', 'action' => 'dashboard_add', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)), array('id' => 'category-' . $row['Taxonomy']['id'], 'class' => 'add-sub-category', 'title' => $row['Taxonomy']['name']))
					. '</em>'
					;
				}
				$output.= '</p>'
				. '</td>'
				. '<td class="options">'
				. $this->Html->link(__('Editar', true), array('shop' => Configure::read('Shop.slug'), 'controller' => 'taxonomies', 'action' => 'dashboard_edit', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)), array('class' => 'ico edit'))
				. ' ' . $this->Html->link(__('Eliminar', true), array('shop' => Configure::read('Shop.slug'), 'controller' => 'taxonomies', 'action' => 'dashboard_delete', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)), array('class' => 'ico delete'))
				//. ' | '
				//. ' ' . $this->Html->link(__('Subir', true), array('shop' => Configure::read('Shop.slug'), 'controller' => 'taxonomies', 'action' => 'dashboard_move', 'up', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)), array('class' => 'ico move-up'))
				//. ' ' . $this->Html->link(__('Bajar', true), array('shop' => Configure::read('Shop.slug'), 'controller' => 'taxonomies', 'action' => 'dashboard_move', 'down', $row['Taxonomy']['id'], Security::hash($row['Taxonomy']['id'], 'md5', true)), array('class' => 'ico move-down'))
				. '</td>'
				. '</tr>'
				;
				if (isset($row['children'][0])) {
					$output .= $this->taxonomyAdmin($row['children'], $level+1);
				}
			}
			
			return $output;
		}
	}
